<?php

namespace app\dao\notice;

use app\dao\BaseDao;
use app\model\notice\NoticeOther;
use app\model\notice\NoticeOtherRecord;

class NoticeOtherDao extends BaseDao
{


    public function index(array $where)
    {
        return $this->search()->where('website_id', $where['website_id'])->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        return $this->search()->create($where);
    }

    public function updateDao(array $where)
    {
        return $this->getModel()->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()-> whereIn('id',$where['id']) -> update(['delete_time' => date('Y-m-d H:i:s', time())]);
    }

    public function searchDao(array $where)
    {
        return $this->getModel()::withSearch(['title', 'type'], ['title' => $where['title'],'type' => $where['type'] ]
        ) ->where('website_id',$where['website_id'])  ->paginate($where['pageSize']);
    }

    protected function setModel(): string
    {
        return NoticeOther::class;
    }

    //---------------------------------------api-----------------------------------------------------
    public function getInformaiton(array $where){
        return $this->getModel()
            ->where('type',$where['type'])
            ->where('website_id',$where['website_id'])
            ->order('update_time','asc')
            ->withoutField(['status','content','create_time','update_time','delete_time'])
            ->where('status',1)
            ->select();
    }
    public function getContent(array $where){
        $obj = $where['user_id'];
        $data = $this->getModel()
            ->where('id',$where['id'])
            ->where('status',1)
//            ->where('', 'exp', "FIND_IN_SET($obj,user_ids)")
            ->field(['id','content','create_time'])
            ->find();
        return $data;

    }

    public function insertOther(array $where){
        $datas = $this->getModel()
            ->where('id',$where['id'])
            ->field(['id','content','create_time'])
            ->where('status',1)
            ->find();
        if ($datas){
            $user_ids= ($datas['user_ids']).','.$where['user_id'];
            $n=($user_ids);
            $this->getModel()
                ->where('id',$where['id'])
                ->update([
                    'user_ids'=>$n,
                ]);
            return $datas;
        }
    }
}